package com.dyxy.tms.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import android.util.Log;

public class OkHttpUtils {
	private OkHttpClient mOkHttpClient;
	private String result_get, result_post;
	private static OkHttpUtils okHttpUtils;

	// public OkHttpUtils(String url) {
	// // TODO Auto-generated constructor stub
	// get(url);
	// }
	//
	// public OkHttpUtils(String url, List<Map<String, String>> list,
	// List<String> keys) {
	// // TODO Auto-generated constructor stub
	// post(url, list, keys);
	// }

	public static OkHttpUtils getInstance() {
		if (okHttpUtils == null) {
			okHttpUtils = new OkHttpUtils();
		}
		return okHttpUtils;
	}

	public String getresult() {
		return result_get;
	}

	public String get(String url) {
				mOkHttpClient = new OkHttpClient();
				Request request = new Request.Builder().url(url).build();
				Response response = null;
				try {
					response = mOkHttpClient.newCall(request).execute();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				Log.d("www", "aaaa"+response.toString());
				if (response.isSuccessful()) {
					try {
						Log.d("www", response.body().string());
						return response.body().string();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				return null;
//		// 创建okHttpClient对象
//		OkHttpClient mOkHttpClient = new OkHttpClient();
//		// 创建一个Request
//		Request request = new Request.Builder().url(url).build();
//		// new call
//		Call call = mOkHttpClient.newCall(request);
//		// 请求加入调度
//		call.enqueue(new Callback() {
//			@Override
//			public void onFailure(Request request, IOException e) {
//			}
//
//			@Override
//			public void onResponse(final Response response) throws IOException {
//				// String htmlStr = response.body().string();
//				result_get = response.body().string();
//			}
//		});
	}

	/**
	 * 
	 * list : 存放post信息 keys : 存放post key
	 * 
	 */

	public void post(String url, List<Map<String, String>> list, List<String> keys) {
		if (result_post != null) {
			result_post = null;
		}
		FormEncodingBuilder builder = new FormEncodingBuilder();
		for (int i = 0; i < list.size(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			String key = keys.get(i);
			map = list.get(i);
			builder.add(key, map.get(key));
		}

		Request request = new Request.Builder().url(url).post(builder.build()).build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {

			@Override
			public void onFailure(Request arg0, IOException arg1) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onResponse(Response arg0) throws IOException {
				// TODO Auto-generated method stub
				result_post = arg0.body().string();
			}
		});
	}

	public String loginJson2Str(String string) {
		try {
			JSONObject jb = new JSONObject(string);
			string = jb.getString("status");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return string;
	}

}
